import React from 'react';
import SearchTable from '@/components/common/SearchTable';
import { connect } from 'dva';

@connect((state: any) => ({
  vehicleInfo: state.vehicleInfo,
  loading: state.loading.effects['vehicleInfo/getFetchPage'],
}))
export default class WarningTab extends React.Component<any, any> {
  onSource = (page, size, args) =>
    new Promise(async resolve => {
      const { vehicleInfo } = this.props;
      const res = await this.props.dispatch({
        type: 'vehicleInfo/getFetchPage',
        payload: {
          url: '/simcard/thresholdnotifypage',
          // iccid: '89860921750008403625',
          iccid: vehicleInfo.iccid,
          current: page,
          pageSize: size,
          ...args,
        },
      });

      if (res && res.statusCode === '0') {
        resolve({
          total: res.data.pagination.total,
          data: res.data.list,
        });
      } else {
        resolve({
          total: 0,
          data: [],
        });
      }
    });
  //流量kb转换成mb函数
  bytesToSize(sizes) {
    let mYsize = sizes;

    mYsize = parseFloat((mYsize / 1024).toFixed(2));

    return mYsize;
  }

  render() {
    const {
      vehicleInfo: {
        pageData: { pagination },
      },
    } = this.props;
    const columns = [
      {
        title: '序号',
        dataIndex: 'index',
        render: (text: any, record: any, index: any) => {
          return index + 1 + (pagination.current - 1) * pagination.pageSize;
        },
      },
      {
        title: '推送时间',
        dataIndex: 'createTime',
      },
      {
        title: '类型',
        dataIndex: 'type',
        render: (text: any) => {
          return text === 0 ? '用量通知' : '时长通知';
        },
      },
      {
        title: '商品名称',
        dataIndex: 'goodsName',
      },
      {
        title: '总流量(M)',
        dataIndex: 'totalData',
        render: (text: any) => {
          return text !== '0' ? this.bytesToSize(text) : text;
        },
      },
      {
        title: '剩余流量(M)',
        dataIndex: 'totalDataRemain',
        render: (text: any) => {
          return text !== '0' ? this.bytesToSize(text) : text;
        },
      },
      {
        title: '当前总用量(M)',
        dataIndex: 'totalDataUsed',
        render: (text: any) => {
          return text !== '0' ? this.bytesToSize(text) : text;
        },
      },
      {
        title: '用量百分比',
        dataIndex: 'percent',
      },
      {
        title: '到期时间',
        dataIndex: 'expiredTime',
      },
    ];
    return (
      <>
        <SearchTable
          tableProps={{
            rowKey: 'id',
            scroll: { x: true },
            columns: columns,
            onSource: this.onSource,
          }}
        />
      </>
    );
  }
}
